cmake_minimum_required(VERSION 3.16)
project(c_backend)

enable_testing()

# Set up language settings
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

# Find Halide
find_package(Halide REQUIRED)

# Generator(s)
add_executable(pipeline.generator pipeline_generator.cpp)
target_link_libraries(pipeline.generator PRIVATE Halide::Generator)

add_halide_library(pipeline_c FROM pipeline.generator
                   C_BACKEND
                   GENERATOR pipeline)
add_halide_library(pipeline_native FROM pipeline.generator
                   GENERATOR pipeline)

add_executable(pipeline_cpp.generator pipeline_cpp_generator.cpp)
target_link_libraries(pipeline_cpp.generator PRIVATE Halide::Generator)

add_halide_library(pipeline_cpp_cpp FROM pipeline_cpp.generator
                   C_BACKEND
                   GENERATOR pipeline_cpp
                   FEATURES c_plus_plus_name_mangling)
add_halide_library(pipeline_cpp_native FROM pipeline_cpp.generator
                   GENERATOR pipeline_cpp
                   FEATURES c_plus_plus_name_mangling)

# Final executable(s)
add_executable(run_c_backend_and_native run.cpp)
target_link_libraries(run_c_backend_and_native
                      PRIVATE
                      pipeline_native
                      pipeline_c)

add_executable(run_c_backend_and_native_cpp run_cpp.cpp)
target_link_libraries(run_c_backend_and_native_cpp
                      PRIVATE
                      pipeline_cpp_native
                      pipeline_cpp_cpp)

# Test that the app actually works!
add_test(NAME c_backend COMMAND run_c_backend_and_native)
add_test(NAME c_backend_cpp COMMAND run_c_backend_and_native_cpp)

set_tests_properties(c_backend c_backend_cpp PROPERTIES
                     LABELS internal_app_tests
                     PASS_REGULAR_EXPRESSION "Success!"
                     SKIP_REGULAR_EXPRESSION "\\[SKIP\\]")
